package com.ey12.service;

import java.util.ArrayList;
import java.util.List;

import com.ey12.db.dao.GmCserverDAO;
import com.ey12.db.dao.GmGameDAO;
import com.ey12.db.dao.GmGameStoreDAO;
import com.ey12.db.domain.GmCserver;
import com.ey12.db.domain.GmGame;
import com.ey12.db.domain.GmGameStore;
import com.ey12.db.domain.GmOrder;
import com.ey12.db.domain.GmUser;
import com.ey12.utils.DateUtil;

public class GmGameStoreService {
	private GmGameStoreDAO gmGameStoreDAO;
	private GmGameDAO gmGameDAO;
	private GmCserverDAO gmCserverDAO;

	public void setGmGameStoreDAO(GmGameStoreDAO gmGameStoreDAO) {
		this.gmGameStoreDAO = gmGameStoreDAO;
	}

	public GmGameStoreDAO getGmGameStoreDAO() {
		return gmGameStoreDAO;
	}

	public List findGoodData(GmUser guser) {

		List list = gmGameStoreDAO.findGood(guser.getUserName());
		System.out.println("findGoodData="+list.size());
		return switchCserverValues(list);
	}
	
	public GmGameStore getGood(int id){
		GmGameStore gamestore = gmGameStoreDAO.findById(id);
		if(gamestore.getIsOnline()==1&&gamestore.getGmUser().getIsOnline()==1&&gamestore.getIsDel()==1){
			return gamestore;
		}
		else{
			return null;
		}
	}

	public List getmembusinese() throws Exception{
		try{
		List list = gmGameStoreDAO.findAll();
		if(list.size()>0){
//			System.out.println(list.size());
			return switchCserverValues(list);
		}
		return null;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getMyGoodDetial(GmGameStore gamestore){
		List list = gmGameStoreDAO.findMyGoodDetial(gamestore);
		if(list.size()>0){
			return switchCserverValues(list);
		}
		return null;
	}
	
	public List searchMembusinese(String gameName,String userName,String gameServer){
		List list = gmGameStoreDAO.findMembusinese(gameName,userName,gameServer);
		if(list.size()>0){
			return list;
		}
		return null;
	}
	public String deleteGoods(List list){
		if(list.size()>0){			
			for(int i=0;i<list.size();i++){
				GmGameStore g = (GmGameStore)list.get(i);				
				g.setIsDel(new Integer(2));
				gmGameStoreDAO.saveOrUpdate(g);			
			}
			return "success";
		}
		return null;
	}
	public List offLines(List list){
		if(list.size()>0){
			List result = new ArrayList();
			for(int i=0;i<list.size();i++){
				GmGameStore g = (GmGameStore)list.get(i);
				g.setIsOnline(new Integer(2));
				gmGameStoreDAO.saveOrUpdate(g);
				result.add(g);
			}
			return result;
		}
		return null;
	}
	public List delLines(List list){
		if(list.size()>0){
			List result = new ArrayList();
			for(int i=0;i<list.size();i++){
				GmGameStore g = (GmGameStore)list.get(i);
				g.setIsDel(new Integer(2));
				gmGameStoreDAO.saveOrUpdate(g);
				result.add(g);
			}
			return result;
		}
		return null;
	}
	public List onLines(List list){
		if(list.size()>0){
			List result = new ArrayList();
			for(int i=0;i<list.size();i++){
				GmGameStore g = (GmGameStore)list.get(i);
				g.setIsOnline(new Integer(1));
				gmGameStoreDAO.saveOrUpdate(g);
				result.add(g);
			}
			return result;
		}
		return null;
	}
	
	public void saveGameStore(GmGameStore g){
		gmGameStoreDAO.saveOrUpdate(g);
	}
	
	public void addNewGood(GmGameStore gameStore) {
		List list = gmGameStoreDAO.findTheSame(gameStore);
		if(list.size()>0){
			GmGameStore gm = (GmGameStore)list.get(0);
			gmGameStoreDAO.delete(gm);
		}
		
		List list1 = gmGameDAO.findByGameName(gameStore.getGmGame()
				.getGameName());
		if (list1.size() > 0) {
			GmGame game = (GmGame) list1.get(0);
			gameStore.setGmGame(game);
		}
		
		List list2 = gmCserverDAO.findUnit(gameStore.getGmCserver(), gameStore
				.getGmGame().getGameName());
		if (list2.size() > 0) {
			GmCserver cserver = (GmCserver) list2.get(0);
			gameStore.setGmCserver(cserver);
//			System.out.println(cserver.getId());
		}
		
		/*
		 * GmGameStore cp = new GmGameStore();
		 * cp.setGmUser(gameStore.getGmUser()); System.out.println("bbbbb");
		 * cp.getGmGame().setGameName(gameStore.getGmGame().getGameName());
		 * cp.getGmCserver
		 * ().setCserverName(gameStore.getGmCserver().getCserverName());
		 * cp.getGmCserver
		 * ().setSeverType(gameStore.getGmCserver().getSeverType());
		 * cp.getGmCserver().setUnit(gameStore.getGmCserver().getUnit());
		 * System.out.println("aaaa"); cp.setPrice(gameStore.getPrice());
		 * cp.setStoreNum(gameStore.getStoreNum());
		 * cp.setLeastMount(gameStore.getLeastMount());
		 * cp.setIsDeposit(gameStore.getIsDeposit());
		 * cp.setIsQuick(gameStore.getIsQuick());
		 */
		gameStore.setUpdateDate(new DateUtil().toTimeString());
		gameStore.setId(null);
		gameStore.setIsDel(new Integer(1));
		gameStore.setIsOnline(new Integer(1));
		
		gmGameStoreDAO.saveOrUpdate(gameStore);
	}

	public List getGameDetialData(String gameName, String cserverName,
			String serverType) {
		List games = gmGameStoreDAO.findGameDetialData(gameName, cserverName,
				serverType);
	
		return switchCserverValues(games);
	}
	public List getLoginGameDetialData(String gameName, String cserverName,
			String serverType,String unit) {
//		System.out.println("getLoginGameDetialData used");
		List games = gmGameStoreDAO.findLoginGameDetialData(gameName, cserverName,
				serverType,unit);
//		System.out.println("games1234" + games.size());
		return switchCserverValues(games);
	}

	private List switchCserverValues(List list) {
		List result = new ArrayList();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				GmGameStore gm = (GmGameStore) list.get(i);
				if (gm.getIsDeposit() != null) {
					if (gm.getIsDeposit() == 2) {
						gm.setGoodState("赔");
					} 
				}
				
				if (gm.getIsQuick() != null) {
					if (gm.getIsQuick() == 2) {
//						System.out.println(gm.getGoodState());
						gm.setGoodState(gm.getGoodState() +"闪");
					}
				}
				if(gm.getGoodState()==null||gm.getGoodState()==""){
					gm.setGoodState("普");
				}
//				System.out.println("-"+gm.getGoodState());
				result.add(gm);
				gm = null;
			}
		}

		return result;
	}

	

	public void setGmGameDAO(GmGameDAO gmGameDAO) {
		this.gmGameDAO = gmGameDAO;
	}

	public GmGameDAO getGmGameDAO() {
		return gmGameDAO;
	}

	public void setGmCserverDAO(GmCserverDAO gmCserverDAO) {
		this.gmCserverDAO = gmCserverDAO;
	}

	public GmCserverDAO getGmCserverDAO() {
		return gmCserverDAO;
	}

}
